﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Star : Item
{
    private SaveManager saveManager;
    public ParticleSystem m_ParticleSystem;

    public int index;    

    protected override void OnEnable()
    {
        base.OnEnable();
        eventSystem.TouchCheckPoint += OnTouchCheckPoint;
        eventSystem.BeforeLoadScene += BeforeLoadScene;
    }
    protected override void OnDisable()
    {
        base.OnEnable();
        eventSystem.TouchCheckPoint -= OnTouchCheckPoint;
        eventSystem.BeforeLoadScene -= BeforeLoadScene;
    }
    private void Start()
    {
        if (index < 0 || index >= SaveData.STAR_NUM)
            Destroy(gameObject);
        saveManager =  Service.Get<GameManager>().saveManager;
        if (saveManager.SaveData.Stars_Destroyed[index])
            Destroy(gameObject);
    }

    protected override void OnPicked(Character player)
    {
        Service.Get<AudioPlayer>().CreateAudio("AudioSource/fx_getstar");
        m_ParticleSystem.Play();
    }

    //到达存档点或过关时才更新星星收集情况
    protected void OnTouchCheckPoint(Checkpoint checkpoint)
    {
        UpdateStar();
    }
    protected void BeforeLoadScene(int index)
    {
        UpdateStar();
    }

    private void UpdateStar()
    {
        if (Picked)
        {
            saveManager.UpdateStar(index, true);
            Destroy(gameObject);
        }
    }
}
